import { SwitchField } from '@aws-amplify/ui-react'; import { DefaultSwitchFieldExample, DisabledSwitchFieldExample, SwitchFieldDefaultCheckedExample, SwitchFieldErrorExample, SwitchFieldIsCheckedExample, SwitchFieldLabelExample, SwitchFieldLabelHiddenExample, SwitchFieldLabelPositionExample, SwitchFieldNameExample, SwitchFieldOnChangeExample, SwitchFieldSizeExample, SwitchFieldStylePropsExample, SwitchFieldThemeExample, SwitchFieldThumbColorExample, SwitchFieldTrackCheckedColorExample, SwitchFieldTrackColorExample, SwitchFieldValueExample, } from './examples'; import { SwitchDemo, SwitchExample, ChangeExample } from './demo'; import { Example, ExampleCode } from '@/components/Example'; import { Fragment } from '@/components/Fragment'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import ThemeExample from '@/components/ThemeExample.mdx'; ## Demo ## Usage The most basic usage simply includes a SwitchField component passing in a required label prop. ```jsx file=./examples/DefaultSwitchFieldExample.tsx ``` ### Accessibility / Label behavior {() => import('./../shared/formFieldAccessibility.mdx')} ### Controlled component The `SwitchFIeld` can be a controlled component by passing in the controlled boolean value as the `isChecked` prop. To allow the user to toggle a controlled `SwitchField`, the `onChange` handler must be passed in and update the controlled value. An example of this pattern is displayed below. ```jsx file=./examples/SwitchFieldIsCheckedExample.tsx ``` ### defaultChecked The `defaultChecked` property is a `boolean` value and will define the starting value for an uncontrolled switchField. ```jsx file=./examples/SwitchFieldDefaultCheckedExample.tsx ``` ### thumbColor The `thumbColor` property is a CSS color value and will define the color of the thumb in the switchField. ```jsx file=./examples/SwitchFieldThumbColorExample.tsx ``` ### trackColor The `trackColor` property is a CSS color value that will define the color of the track of the switchField while in the off position. ```jsx file=./examples/SwitchFieldTrackColorExample.tsx ``` ### trackCheckedColor The `trackCheckedColor` property is a CSS color value that will define the color of the track of the switchField while in the on position. ```jsx file=./examples/SwitchFieldTrackCheckedColorExample.tsx ``` ### isDisabled The `isDisabled` property is a `boolean` value that when set to true will disable the switchField from being toggled. ```jsx file=./examples/DisabledSwitchFieldExample.tsx ``` ### name The `name` property is a `string` that defines the name of the field that will be submitted with the form as a name/value pair. ```jsx file=./examples/SwitchFieldNameExample.tsx ``` ### size The `size` property is an `enum` value that modifies the size of the switchField component. The available sizes are `small`, (default), and `large`. ```jsx file=./examples/SwitchFieldSizeExample.tsx ``` ### label The `label` property is a required `string` or `ReactNode` that will display next to the switchField component and wrapped in an html label tag. ```jsx file=./examples/SwitchFieldLabelExample.tsx ``` ### labelPosition The `labelPosition` property is an `enum` value that defines the label's position in relation to the switchField. Available values are `start`, `end`, `top`, and `bottom`. ```jsx file=./examples/SwitchFieldLabelPositionExample.tsx ``` ### isLabelHidden The `isLabelHidden` property is a `boolean` value that will visually hide the label. ```jsx file=./examples/SwitchFieldLabelHiddenExample.tsx ``` ### value The `value` property is a `string` value that defines the value of the field that will be submitted with the form as a name/value pair. ```jsx file=./examples/SwitchFieldValueExample.tsx ``` ### onChange The `onChange` property is a callback `function` that will be called with a change event to the switchField. ```jsx file=./examples/SwitchFieldOnChangeExample.tsx ``` ### Error state ```jsx file=./examples/SwitchFieldErrorExample.tsx ``` ## CSS Styling ```tsx file=./examples/SwitchFieldThemeExample.tsx ``` ### Target classes ### Global Styling To override styling on all SwitchFields, you can set the Amplify CSS variables or use the built-in `.amplify-switchfield` class. ```css /* styles.css */ :root { --amplify-components-switchfield-default-font-size: 0.5rem; } /* OR */ .amplify-switchfield { font-size: 0.5rem; } ``` ### Local styling To override styling on a specific SwitchField, you can use (in order of increasing specificity): a class selector, data attributes, or style props. _Using a class selector:_ ```css /* styles.css */ .my-custom-switchfield { font-size: 0.75rem; } ``` ```jsx import './styles.css'; ; ``` _Using data attributes:_ ```css /* styles.css */ /* Override only large size styles */ .amplify-switchfield[data-size='large'] { font-size: 2.5rem; } ``` ```jsx import './styles.css'; ; ``` _Using style props:_ ```jsx file=./examples/SwitchFieldStylePropsExample.tsx ```